//
//  LPActivityDetailCourseStudyViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/9.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPActivityDetailCourseStudyViewController.h"
#import "LPActivityCouseStudyClassTableViewCell.h"
//视频观看
#import "StudyCoureVideoViewController.h"

/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#import "ZJStudyRecommendModel.h"

@interface LPActivityDetailCourseStudyViewController ()<UITableViewDelegate,UITableViewDataSource,LPActivityCouseStudyClassDelegate>
/**  tableView  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/**  是否显示更多  */
@property (assign, nonatomic) BOOL   isShowMore;
/**  是否展开  */
@property (assign, nonatomic) BOOL  isFlod;
/**  yeshu  */
@property (assign, nonatomic) NSInteger  page;

/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
@end

@implementation LPActivityDetailCourseStudyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"学习列表";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    [self.view addSubview:self.tableView];
}
#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"AI_ID"] = self.model.ID;
    paramer[@"USERID"] = ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID;
    paramer[@"PAGE"] = page;
    paramer[@"COUNT"] = @"10";

    NSString *urlstring;
    if (self.isSeeMore) {
        urlstring = [NetWorkingURL getactmactionActStudyList];
    }else{
        urlstring = [NetWorkingURL getactmactionActStudyListnoFinishStudy];
    }
    [NetWorkingManager postWithUrlString:urlstring Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"STUDYINFO"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array] mutableCopy];
            [weakSelf.tableView reloadData];
            if (weakSelf.dataSouce.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
            }
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf.tableView.mj_header beginRefreshing];
            }];
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
    }];
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"AI_ID"] = self.model.ID;
    paramer[@"USERID"] = ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID;
    paramer[@"PAGE"] = page;
    paramer[@"COUNT"] = @"10";

    NSString *urlstring;
    if (self.isSeeMore) {
        urlstring = [NetWorkingURL getactmactionActStudyList];
    }else{
        urlstring = [NetWorkingURL getactmactionActStudyListnoFinishStudy];
    }
    [NetWorkingManager postWithUrlString:urlstring Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"STUDYINFO"];
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array]];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}




#pragma mark - 界面
- (void)layoutUI{
    
    [self.view addSubview:self.tableView];
}



#pragma mark - LPActivityCouseStudyClassDelegate
- (void)lpActivityCouseStudyClassWithCell:(LPActivityCouseStudyClassTableViewCell *)cell isClickFlod:(BOOL)flod row:(NSInteger)row{
    self.isFlod = flod;
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    
}
- (void)lpActivityCouseStudyClassWithCell:(LPActivityCouseStudyClassTableViewCell *)cell row:(NSInteger)row{

}

- (void)lpActivityCouseStudyClassWithCell:(LPActivityCouseStudyClassTableViewCell *)cell isSeeMore:(BOOL)more row:(NSInteger)row{
    self.isShowMore = more;
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    

}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ZJStudyRecommendModel * model = self.dataSouce[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
        ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyBookTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }else{
        ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudyRecommendModel *model = self.dataSouce[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        return 125 * [UIScreen zj_screenHScale];//图书
    }
    return 120 * [UIScreen zj_screenHScale];//视频、音频
    
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
    vc.model = self.dataSouce[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}


- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
